package jjoms.controller;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import jjoms.model.qna.QnaService;
import jjoms.model.qna.QnaVO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/qna")
public class QnaController {
	@Resource(name="qnaServiceImpl")
	private QnaService qnaService;
	private Log log=LogFactory.getLog(getClass());
	
	@RequestMapping("/{viewId}_view.moja")
	public String showView(@PathVariable("viewId") String viewId) {
		System.out.println("showView : " + viewId);
		return viewId + "_view.qna";
	}
	@RequestMapping("/getQnaList.moja")
	public ModelAndView getQnaList(String pageNo){
	
		return new ModelAndView("getQnaList.qna","list",qnaService.getallQnaList(pageNo));
	}
	@RequestMapping("/qnaWrite.moja")
	public String qnaWrite(QnaVO qvo){
		qnaService.qnaWrite(qvo);
		return "redirect:getQnaList.moja";
	}
	@RequestMapping("/showQnaContent.moja")
	public ModelAndView showQnaContent(int qnaNo,@CookieValue(value="qnaBoard",required=false)
	String cookieValue,HttpServletResponse response){
	
		QnaVO qvo=null;
		if(cookieValue==null){
			log.info("qnaBoard cookie 존재하지 않으므로 cookie 생성하고 count 증가");
			response.addCookie(new Cookie("qnaBoard","|"+qnaNo+"|"));
			qvo=qnaService.showQnaContent(qnaNo);
		}else if(cookieValue.indexOf("|"+qnaNo+"|")==-1){
			log.info("qnaBoard cookie 존재하지만 "+qnaNo+"번글은 처음 조회하므로 count증가");
			cookieValue+="|"+qnaNo+"|";
			//글번호를 쿠키에 추가 
			response.addCookie(new Cookie("qnaBoard",cookieValue));
			qvo=qnaService.showQnaContent(qnaNo);
		}else{
			log.info("qnaBoard cookie 존재하고 이전에 해당 게시물 읽었으므로 count 증가x");
			qvo=qnaService.showQnaContentNoHit(qnaNo);
		}		
		return new ModelAndView("showQnaContent.qna","qvo",qvo);
		
		
	}
	
	@RequestMapping("/showQnaContentNoHit.moja")
	public ModelAndView showQnaContentNoHit(int qnaNo) {			
		return new ModelAndView("showQnaContent.qna","qvo",qnaService.showQnaContentNoHit(qnaNo));
	}
	@RequestMapping("/qnaUpdateView.moja")
	public ModelAndView qnaUpdateView(int qnaNo){
		QnaVO qvo=qnaService.showQnaContentNoHit(qnaNo);
		return new ModelAndView("qnaUpdateView.qna","qvo",qvo);
	}
	@RequestMapping("/qnaUpdate.moja")
	public ModelAndView qnaUpdate(QnaVO qvo){
		System.out.println(qvo);
		qnaService.qnaUpdate(qvo);
		return new ModelAndView("showQnaContent.qna","qvo",qnaService.showQnaContentNoHit(qvo.getQnaNo()));
	}
	@RequestMapping("/qnaDelete.moja")
	public String qnaDelete(int qnaNo){
		qnaService.qnaDelete(qnaNo);
		return "redirect:getQnaList.moja";
	}
	@RequestMapping("/qnaReplyView.moja")
	public ModelAndView replyView(int qnaNo) {		
		return new ModelAndView("reply_form.qna","qvo",
				qnaService.showQnaContentNoHit(qnaNo));
	}
	@RequestMapping("/reply.moja")
	public ModelAndView reply(QnaVO qvo) throws Exception{		
		qnaService.reply(qvo);		
		return new ModelAndView("redirect:showQnaContentNoHit.moja?qnaNo="+qvo.getQnaNo());
	}
}
